package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.PurchaseAmountBo;
import com.ruoyi.supplier.domain.vo.PurchaseAmountVo;
import com.ruoyi.supplier.domain.PurchaseAmount;
import com.ruoyi.supplier.mapper.PurchaseAmountMapper;
import com.ruoyi.supplier.service.IPurchaseAmountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采购单费用结算Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class PurchaseAmountServiceImpl implements IPurchaseAmountService {

    private final PurchaseAmountMapper baseMapper;

    /**
     * 查询采购单费用结算
     */
    @Override
    public PurchaseAmountVo queryById(Integer purchaseAmountId){
        return baseMapper.selectVoById(purchaseAmountId);
    }

    /**
     * 查询采购单费用结算列表
     */
    @Override
    public TableDataInfo<PurchaseAmountVo> queryPageList(PurchaseAmountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PurchaseAmount> lqw = buildQueryWrapper(bo);
        Page<PurchaseAmountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采购单费用结算列表
     */
    @Override
    public List<PurchaseAmountVo> queryList(PurchaseAmountBo bo) {
        LambdaQueryWrapper<PurchaseAmount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PurchaseAmount> buildQueryWrapper(PurchaseAmountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PurchaseAmount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMerchantId() != null, PurchaseAmount::getMerchantId, bo.getMerchantId());
        lqw.eq(bo.getSupplierId() != null, PurchaseAmount::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getStatus() != null, PurchaseAmount::getStatus, bo.getStatus());
        lqw.eq(bo.getShouldAmount() != null, PurchaseAmount::getShouldAmount, bo.getShouldAmount());
        lqw.eq(bo.getActualAmount() != null, PurchaseAmount::getActualAmount, bo.getActualAmount());
        lqw.eq(bo.getAccountantId() != null, PurchaseAmount::getAccountantId, bo.getAccountantId());
        return lqw;
    }

    /**
     * 新增采购单费用结算
     */
    @Override
    public Boolean insertByBo(PurchaseAmountBo bo) {
        PurchaseAmount add = BeanUtil.toBean(bo, PurchaseAmount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPurchaseAmountId(add.getPurchaseAmountId());
        }
        return flag;
    }

    /**
     * 修改采购单费用结算
     */
    @Override
    public Boolean updateByBo(PurchaseAmountBo bo) {
        PurchaseAmount update = BeanUtil.toBean(bo, PurchaseAmount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PurchaseAmount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采购单费用结算
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
